<template>
    <div>
        <el-alert title="注意:只允许为第三级分类设置相关参数！" type="warning" show-icon>
        </el-alert>
        <p style="margin:15px 0 ;">选择商品分类：<el-cascader v-model="value" :options="options"
                :props="{ expandTrigger: 'hover', value: 'cat_id', label: 'cat_name' }"
                @change="handleChange"></el-cascader>
        </p>
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane :label="item.name" :name="item.label" v-for="(item, index) in tab" :key="index">
                <el-button type="primary" @click="add()" style="margin-bottom:15px;"
                    :disabled="value == '' ? true : false">{{ activeName == 'many' ? '添加参数' :
                        '添加属性'
                    }}</el-button>
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column type="expand">
                        <template slot-scope="props">
                            <el-tag :key="tag" v-for="tag in props.row.attr_vals" closable :disable-transitions="false"
                                @close="handleClose(tag)">
                                {{ tag }}
                            </el-tag>
                            <el-input class="input-new-tag" v-if="props.row.inputVisible" v-model="inputValue"
                                ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(props.row)"
                                @blur="handleInputConfirm(props.row)">
                            </el-input>
                            <el-button v-else class="button-new-tag" size="small" @click="showInput(props.row)">+ New
                                Tag</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column type="index" label="#" width="50">
                    </el-table-column>
                    <el-table-column prop="attr_name" label="参数名称">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>

        </el-tabs>

        <el-dialog :title="activeName ? '添加动态参数' : '添加静态属性'" :visible.sync="dialogTableVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item :label="activeName ? '动态参数' : '静态属性'" prop="attr_name">
                    <el-input v-model="ruleForm.attr_name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-dialog :title="activeName ? '修改动态参数' : '修改静态属性'" :visible.sync="dialogFormVisible1">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item :label="activeName ? '动态参数' : '静态属性'" prop="attr_name">
                    <el-input v-model="ruleForm.attr_name"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="edit">确 定</el-button>
            </div>
        </el-dialog>
        <!-- <paramsye :activeName="activeName" :value="value" :tableData="tableData" :options="options"></paramsye> -->
    </div>
</template>

<script>
// import paramsye from '@/components/paramsye.vue';
import { categories, Attcategories, logCategories, tableCategories, deleteCategories, editCategories } from '../untils/api'
export default {
    name: 'params',
    // components:{paramsye},
    data() {
        return {
            value: [],
            activeName: 'many',
            tableData: [],
            options: [],
            tab: [{ name: '动态参数', label: 'many' }, { name: '静态属性', label: 'only' }],
            tableData: [],
            tagRow: {},

            inputValue: '',
            row: {},
            fenId: '',
            Id: '',
            index: '',
            ruleForm: {
                attr_name: '',
            },
            dialogTableVisible: false,
            dialogFormVisible1: false,
            rules: {
                attr_name: [
                    { required: true, message: '请输入参数', trigger: 'blur' },

                ]
            }


        }
    },
    methods: {
        //关闭标签
        handleClose(tag) {
            // 删除参数

        },
        showInput(row) {
            // console.log(row);
            // this.tagRow = row
            row.inputVisible = true;
            // console.log(this.$refs.saveTagInput);
            this.$nextTick(_ => {

                // console.log(this,"111")
                // console.log(this.$refs.saveTagInput,"$$$")

                this.$refs.saveTagInput[0].$refs.input.focus();
            });
        },

        handleInputConfirm(row) {
            console.log(row);
            let inputValue = this.inputValue;

            if (inputValue) {
                logCategories({ attr_name: inputValue, id: row.attr_id, attr_sel: row.attr_sel, attr_vals: inputValue }).then(res => {
                    console.log(res);
                    row.attr_vals.push(res.data.data.attr_name)
                })
            }
            row.inputVisible = false;
            this.inputValue = '';
        },

        add() {//添加动态参数和静态属性
            this.dialogTableVisible = true

        },
        submitForm(formName) {//添加确定
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let arr = {
                        attr_name: this.ruleForm.attr_name,
                        attr_sel: this.activeName,
                        attr_vals: '',
                        id: this.fenId
                    }
                    // console.log(arr);
                    // let as = this.tableData.findIndex(item => { item.attr_name == arr.attr_name })
                    // if (as == -1) {
                    //     this.$message({
                    //         type: 'warning',
                    //         message: '商品分类已存在!'
                    //     });
                    //     return false
                    // } else {
                    logCategories(arr).then(res => {
                        // console.log(res);
                        this.tableData.push(res.data.data)
                        // this.package()
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
            this.dialogTableVisible = false
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },

        handleClick(tab, event) { //
            // console.log(tab, event);
            this.activeName = tab._props.name
            let Id = this.value[2]
            Attcategories({ sel: this.activeName, id: Id }).then(res => {
                // console.log(res);
                res.data.data.forEach(item => {
                    item.attr_vals = item.attr_vals.split(',')
                    item.inputVisible = false
                })
                this.tableData = res.data.data
            })
        },
        handleChange(value) {//三级联动
            Attcategories({ id: this.value[2], sel: this.activeName }).then(res => {
                // console.log(res);
                res.data.data.forEach(item => {
                    item.attr_vals = item.attr_vals.split(',')
                    item.inputVisible = false
                })
                this.tableData = res.data.data

            })
        },
        //编辑和删除
        handleEdit(index, row) {
            // console.log(row);
            this.dialogFormVisible1 = true;
            this.ruleForm.attr_name = row.attr_name
            this.row = row
            this.index = index
            // console.log(this.index);
            // console.log(index, row);
        },
        edit() {//修改

            editCategories({
                ...this.ruleForm,
                id: this.row.cat_id,
                attrid: this.row.attr_id,
                attr_sel: this.row.attr_sel,
                attr_vals: this.row.attr_vals
            }).then(res => {
                // console.log(res);
                // this.package()
                // this.handleClick()
                // this.$set(this.tableData[this.index], res.data.data)
                // this.tableData[this.index] = res.data.data
                // this.package()
                Attcategories({ id: this.row.cat_id, sel: this.row.attr_sel }).then(res => {
                    // console.log(res);
                    res.data.data.forEach(item => {
                        item.inputVisible = false
                    })
                    this.tableData = res.data.data

                })

            })
            this.dialogFormVisible1 = false;
        },
        handleDelete(index, row) {//删除
            // console.log(row);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCategories({ id: row.cat_id, attrid: row.attr_id }).then(res => {
                    // console.log(res);
                    // this.package()
                })
                // this.$message({
                //     type: 'success',
                //     message: '删除成功!'
                // });
                this.tableData.splice(index, 1) //删除页面的数据
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

            // console.log(index, row);
        },
        package() {//数据请求
            categories().then(res => {
                // console.log(res);
                this.options = res.data.data
            })
        },
    },
    created() {
        this.package()
    }
}
</script>
<style lang="scss" scoped>
.el-tag+.el-tag {
    margin-left: 10px;
}

.button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}

.input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
}
</style>


